package com.gluck.cases.app.biz.impl.impl;

import com.github.pagehelper.PageInfo;
import com.gluck.cases.app.biz.impl.AdminUserService;
import com.gluck.cases.app.biz.impl.BasecService;
import com.gluck.cases.common.utils.enums.BizSerErrorEnum;
import com.gluck.cases.common.utils.utils.AesCipherUtil;
import com.gluck.cases.common.utils.utils.AssertUtil;
import com.gluck.cases.common.utils.utils.JedisUtil;
import com.gluck.cases.common.utils.utils.JwtUtil;
import com.gluck.cases.core.block.AdminUserBlock;
import com.gluck.cases.core.modal.assemble.AdminUserAssemble;
import com.gluck.cases.core.modal.constants.AuthConstants;
import com.gluck.cases.core.modal.dto.AdminUserDTO;
import com.gluck.cases.core.modal.entity.AdminUser;
import com.gluck.cases.core.modal.req.AdminUserReq;
import com.gluck.cases.core.modal.req.AdminUserSearchReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName AdminUserServiceImpl
 * @Desc AdminUserService 实现类
 * @Author I am Gluck
 * @Date 2022/5/16 - 4:29 下午
 **/
@Service
public class AdminUserServiceImpl extends BasecService implements AdminUserService {

    /**
     * RefreshToken过期时间
     */
    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    @Autowired
    private AdminUserBlock adminUserBlock;

    @Override
    public boolean existAdminUserByUsername(String username) {
        AdminUser adminUser = adminUserBlock.queryByUsername(username);
        return Objects.isNull(adminUser) ? false : true;
    }

    @Override
    public boolean createNewUser(AdminUserReq userReq) {
        AdminUserDTO adminUserDTO = new AdminUserDTO();
        adminUserDTO.setUsername(userReq.getUsername());
        adminUserDTO.setPassword(AesCipherUtil.enCrypto(userReq.getPassword()));
        adminUserDTO.setSalt(userReq.getUsername());
        AssertUtil.isFalse(adminUserBlock.createUser(adminUserDTO), BizSerErrorEnum.ADMIN_USER_ADD_ERROR);
        return true;
    }

    @Override
    public boolean queryUser(AdminUserReq userReq) {
        AdminUser adminUser = adminUserBlock.queryUser(userReq.getUsername(), AesCipherUtil.enCrypto(userReq.getPassword()));
        return Objects.isNull(adminUser) ? false : true;
    }

    @Override
    public String buildUserToken(AdminUserReq userReq) {
        // step 1 清楚缓存信息
        String key = AuthConstants.PREFIX_SHIRO_CACHE + userReq.getUsername();
        if(JedisUtil.exists(key)){
            JedisUtil.delKey(key);
        }
        // step 2 刷新缓存时间
        String currentTimeMillis = String.valueOf(System.currentTimeMillis());
        JedisUtil.setObject(AuthConstants.PREFIX_SHIRO_REFRESH_TOKEN + userReq.getUsername(), currentTimeMillis, Integer.parseInt(refreshTokenExpireTime));
        // step 3 生成token
        String token = JwtUtil.sign(userReq.getUsername(), currentTimeMillis);
        return token;
    }

    @Override
    public PageInfo<AdminUser> queryUserList(Integer startPage, Integer endPage) {
        return adminUserBlock.queryListByPageHelper(startPage, endPage);
    }

    @Override
    public AdminUser updateUser(AdminUserReq req) {
        AdminUserDTO dto = AdminUserAssemble.adminUserReqConvertAdminUserDTO(req);
        return adminUserBlock.updateUser(dto);
    }

    @Override
    public boolean deleteUser(Long userId) {
        AdminUser result = adminUserBlock.findOne(userId);
        if(Objects.isNull(result)){
            AssertUtil.isTrue(true, BizSerErrorEnum.DATA_NOT_FOUND);
        }
        AssertUtil.isFalse(adminUserBlock.deleteUser(userId), BizSerErrorEnum.ADMIN_USER_DEL_ERROR);
        return true;
    }

    @Override
    public boolean deleteUsers(List<Long> ids) {
        ids.stream().filter(x->{
            AdminUser one = adminUserBlock.findOne(x);
            if(Objects.isNull(one)){
                return false;
            }
            return true;
        }).forEach(y->{
            adminUserBlock.deleteUser(y);
        });
        return true;
    }

    @Override
    public boolean changePassword(String username, String newPassword) {
        AdminUser adminUser = adminUserBlock.queryByUsername(username);
        if(Objects.isNull(adminUser)){
            AssertUtil.isTrue(true, BizSerErrorEnum.DATA_NOT_FOUND);
        }
        AssertUtil.isFalse(adminUserBlock.changePassword(adminUser.getId(), newPassword), BizSerErrorEnum.ADMIN_USER_CHANGE_PASS_ERROR);
        return true;
    }

    @Override
    public PageInfo<AdminUser> searchUsers(AdminUserSearchReq req) {
        PageInfo<AdminUser> adminUsers = adminUserBlock.searchUsers(req);
        return adminUsers;
    }

    @Override
    public AdminUserDTO getCurrentUserInfo() {
        return getUserInfo();
    }
}
